package com.xwh.b2c.controller;

import com.xwh.b2c.common.Result;
import com.xwh.b2c.dto.MemberBindAccountDTO;
import com.xwh.b2c.dto.MemberLoginDTO;
import com.xwh.b2c.dto.MemberRegisterDTO;
import com.xwh.b2c.dto.MemberUpdatePasswordDTO;
import com.xwh.b2c.entity.Member;
import com.xwh.b2c.service.MemberService;
import com.xwh.b2c.vo.MemberInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author root
 * @since 2025-07-01
 */
@Slf4j
@RestController
@Tag(name = "会员管理")
// 设置请求路径前缀
@RequestMapping("/member")
public class MemberController {

    @Resource
    private HttpServletRequest request;
    @Resource
    private MemberService memberService;


    //键值都是string 可以用 StringRedisTemplate
    @Autowired
    private StringRedisTemplate redisTemplate;
//    @Resource
//    private RedisTemplate redisTemplate;
    @Operation(summary = "用户登录接口",description = "用户登录接口")
    @PostMapping("login")
    //这里的memberLoginDTO请求体就包括了wx.request传递过来的 code dto : 前端传数据给后端
    public Result<String> login(@RequestBody MemberLoginDTO memberLoginDTO){
        log.info("memberLoginDto:{}",memberLoginDTO);
        // 调用服务层处理登录逻辑，获取token
        //访问/member/login 调用login处理登录
        String token = memberService.login(memberLoginDTO);
        // 返回成功结果，包含token
        return Result.success(token);
    }
    @GetMapping("info")
    public Result<MemberInfoVO> info(){
        // 调用服务层获取用户信息
        MemberInfoVO memberInfoVO = memberService.info();
        // 返回成功结果，包含用户信息
        return Result.success(memberInfoVO);
    }
    //putMapping和PostMapping有什么区别
    /*
    PostMapping：一般用于非幂等接口
    PutMapping：用于幂等接口
    幂等接口：执行一次和执行多次结果一致 非幂等接口：执行一次和执行多次结果一致
    * */
    @PutMapping("updatePassword")
    public Result<Object> updatePassWord(@RequestBody MemberUpdatePasswordDTO memberUpdatePasswordDTO){
        // 调用服务层更新密码
        memberService.updatePassword(memberUpdatePasswordDTO);
        // 返回成功结果
        return Result.success();
    }

/*
*
* 手机号注册需求：
* 1.要输入手机号、邮箱、密码
* 2.需要验证手机号和邮箱是否被注册过
* 3.要求手机号是你本人的（验证码）
* 4.邮箱也是本人的（验证码）
*
* 关于验证码的规则：随机六位数x
*
* */
    @PostMapping("register")
    public Result<Member> register(@RequestBody MemberRegisterDTO memberRegisterDTO){
        log.info("memberRegisterDTO:{}",memberRegisterDTO);
        Member member = memberService.register(memberRegisterDTO);
        log.info("controller:member:{}",member);
        return Result.success(member);
    }

    @PostMapping("sendCode")
    public Result<Object> sendCode(@RequestBody MemberRegisterDTO memberRegisterDTO){
        Random random = new Random();
        String code = random.nextInt(888888)+100000 + "";
        log.info("code:{}",code);
        //把验证码写入到redis
        redisTemplate.opsForValue().set("code",code);
        return Result.success();

    }

    @DeleteMapping("delete")
    public Result<Object> delete(@RequestBody List<Long> ids){
        //删一条 删多条
        this.memberService.removeBatchByIds(ids);
        return Result.success();
    }

    //幂等接口 使用PutMapping
    @PutMapping("bindPhoneOrEmailOrOpenId")
    public Result<Object> bindPhoneOrEmailOrOpenId(@RequestBody MemberBindAccountDTO memberBindAccountDTO){
        log.info("memberBindAccountDTO:{}",memberBindAccountDTO);
        this.memberService.bindPhoneOrEmailOrOpenId(memberBindAccountDTO);
        return Result.success();
    }
}
